package com.app.controller;

import com.app.common.Result;
import com.app.model.AdminPermissionModel;
import com.app.model.AdminRoleModel;
import com.app.model.AdminUserModel;
import com.app.server.AdminPermissionService;
import com.app.server.AdminRoleService;
import com.app.server.AdminUserService;
import com.app.utils.CommonHelp;
import com.app.utils.TokenUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 权限表
 */
@Slf4j
@RestController
@RequestMapping("/sbbz/adminPermission")
public class AdminPermissionController {
    @Autowired
    private AdminUserService adminUserService;
    @Autowired
    private AdminPermissionService permissionService;
    @Autowired
    private AdminRoleService adminRoleService;

    //获取(菜单列表或权限管理树) 查MC
    @GetMapping("/getMenuList")
    public Result<Object> getMenuList(HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String adminId = TokenUtils.verify(token);
        if (adminId.equals("false")) {
            return Result.error(405, "token已过期");
        }
        AdminUserModel adminUser = adminUserService.getById(Integer.valueOf(adminId));
        if (adminUser == null) {
            return Result.error(406, "用户不存在");
        }
        AdminRoleModel roleModel = adminRoleService.getById(adminUser.getRoleId());
        QueryWrapper<AdminPermissionModel> qw = new QueryWrapper();
        qw.in("menu_type", "M", "C");
        qw.orderByAsc("sort");
        List<AdminPermissionModel> adminPermissionList = permissionService.list(qw);//查MC
        if (adminPermissionList == null) {
            return Result.error(200, "暂无数据");
        }
        List<AdminPermissionModel> menuList = permissionService.getMenuList(adminPermissionList);
        List<AdminPermissionModel> modelList = new ArrayList<>();
        for (AdminPermissionModel adminPermissionModel : menuList) {
            List<AdminPermissionModel> childList = adminPermissionModel.getChildList();
            List<AdminPermissionModel> childList1 = new ArrayList<>();
            if (childList != null) {
                for (AdminPermissionModel permissionModel : childList) {
                    if (roleModel.getRules().contains(permissionModel.getPerms())) {
                        childList1.add(permissionModel);
                    }
                }
                adminPermissionModel.setChildList(childList1);
            }
            if (adminPermissionModel.getPid() == 0) {
                if (roleModel.getRules().contains(adminPermissionModel.getPerms())) {
                    modelList.add(adminPermissionModel);
                }
            }
        }
        return Result.success(modelList);
    }

    //获取(菜单列表或权限管理树) 查全部
    @GetMapping("/getMenuTree")
    public Result<Object> getMenuTree(Integer roleId, HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String adminId = TokenUtils.verify(token);
        if (adminId.equals("false")) {
            return Result.error(405, "token已过期");
        }
        AdminUserModel adminUser = adminUserService.getById(Integer.valueOf(adminId));
        if (adminUser == null) {
            return Result.error(406, "用户不存在");
        }
        QueryWrapper<AdminPermissionModel> qw = new QueryWrapper<>();
        qw.orderByAsc("sort");
        List<AdminPermissionModel> adminPermissionList = permissionService.list(qw);//全查
        if (adminPermissionList == null) {
            return Result.error(200, "暂无数据");
        }

        List<AdminPermissionModel> menuList = permissionService.getMenuList(adminPermissionList);
        if (roleId != null) {
            AdminRoleModel adminRole = adminRoleService.getById(roleId);
            String rules = adminRole.getRules();
            List<String> roleModelList = new ArrayList<>();
            if (rules != null && !rules.equals("")) {
                String[] roleArr = rules.split(",");
                roleModelList.addAll(Arrays.asList(roleArr));
            }
            for (String roleModel : roleModelList) {
                for (AdminPermissionModel menuAll : adminPermissionList) {
                    String perms = menuAll.getPerms();
                    if (perms.equals(roleModel)) {
                        menuAll.setChecked(true);
                    }
                }
            }
        }
        return Result.success(menuList);
    }

    //删除菜单
    @GetMapping("/getPermissionById")
    public Result<Object> getPermissionById(Integer id, HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String adminId = TokenUtils.verify(token);
        if (adminId.equals("false")) {
            return Result.error(405, "token已过期");
        }
        AdminUserModel user = adminUserService.getById(Integer.valueOf(adminId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        AdminPermissionModel permission = permissionService.getById(id);
        return Result.success(permission);
    }


    // 新增和修改菜单
    @PostMapping("/addAndUpdatePermission")
    public Result addAndUpdatePermission(@RequestBody AdminPermissionModel permissionModel, HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String adminId = TokenUtils.verify(token);
        if (adminId.equals("false")) {
            return Result.error(405, "token已过期");
        }
        AdminUserModel adminUser = adminUserService.getById(Integer.valueOf(adminId));
        if (adminUser == null) {
            return Result.error(406, "用户不存在");
        }

        AdminPermissionModel permission = permissionService.getById(permissionModel.getId());
        if (permission != null) {
            permissionModel.setUpdateTime(CommonHelp.getNowTimestamp());
            permissionService.updateById(permissionModel);
            return Result.success("修改成功");
        } else {
            permissionModel.setCreateTime(CommonHelp.getNowTimestamp());
            permissionService.save(permissionModel);
            return Result.success("添加成功");
        }
    }

    //删除菜单
    @GetMapping("/delPermission")
    public Result<Object> delPermission(Integer id, HttpServletRequest request) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String adminId = TokenUtils.verify(token);
        if (adminId.equals("false")) {
            return Result.error(405, "token已过期");
        }
        AdminUserModel user = adminUserService.getById(Integer.valueOf(adminId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        permissionService.removeById(id);
        return Result.success("删除成功");
    }


}
